package com.woniu108.extra.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu108.carService.commos.dto.StoreAdminDto;
import com.woniu108.carService.commos.dto.StreamInfoDto;
import com.woniu108.carService.commos.exception.WoniuException;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.QRUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.extra.client.PersonClient;
import com.woniu108.extra.form.AddStreamInfoForm;
import com.woniu108.extra.model.Stream;
import com.woniu108.extra.model.StreamInfo;
import com.woniu108.extra.param.AddStreamInfoParam;
import com.woniu108.extra.service.StreamInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 流程信息表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/streamInfo")
@Api(tags = "详细流程信息接口")
public class StreamInfoController {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private PersonClient personClient;
    @Resource
    private StreamInfoService streamInfoService;

    public String getStoreAdminId(String token){
        String refreshToken=redisUtil.getString(token);
        String storeAdminId=JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        return storeAdminId;
    }

    /**
     * 新建详细流程信息
     * @param form 新增流程详细信息
     * @param token
     * @return result 是否正常访问接口结果
     */
    @PostMapping("/addStreamInfo.st")
    @ApiOperation("新建详细流程信息")
    public Result addStreamInfo(@RequestBody AddStreamInfoForm form, @RequestHeader String token){
        String storeAdminId = this.getStoreAdminId(token);
        if(storeAdminId==null) throw new WoniuException("非门店管理员账号",10001);
        Result<StoreAdminDto> result = personClient.getStoreAdminInfoById(storeAdminId);
        if(result.getCode()!=200) throw new WoniuException(result.getMsg(),result.getCode());
        StoreAdminDto storeAdminDto = result.getData();
        if(storeAdminDto.getStoreAdminState()!= StaticData.ADMIN_STATE_NORMAL) throw new WoniuException("管理员状态异常",10001);
        AddStreamInfoParam param= BeanUtil.toBean(form,AddStreamInfoParam.class);
        StreamController controller = new StreamController();
        //新增流程
        Result<Stream> result1 = controller.addStream(form.getStreamName(), token);
        if(result1.getCode()!=200) throw new WoniuException(result1.getMsg(),result1.getCode());
        Stream stream = result1.getData();
        //新增流程的详细步骤信息
        StreamInfo streamInfo = new StreamInfo();
        streamInfo.setStreamId(stream.getStreamId());
        streamInfo.setStreamInfoName(param.getStreamInfoName());
        streamInfoService.save(streamInfo);
        return Result.getResult().setMsg("新增整体流程信息成功");
    }

    /**
     * 根据流程编号获取详细流程信息
     * @param streamId
     * @return List<StreamInfoDto> 流程详细信息列表
     */
    @GetMapping("/streamInfoByStreamId/{streamId}.c")
    @ApiOperation("根据流程编号获取详细流程信息")
    @ApiImplicitParam(name = "streamId", value = "流程编号")
    public Result<List<StreamInfoDto>> streamInfoByStreamId(@PathVariable("streamId")Integer streamId){
        QueryWrapper<StreamInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("stream_id",streamId);
        List<StreamInfo> streamInfos = streamInfoService.list(wrapper);
        List<StreamInfoDto> dtos=new ArrayList<>();
        if(ObjectUtil.isEmpty(streamInfos)) throw new WoniuException("流程详细信息不存在",10001);
        for (StreamInfo streamInfo : streamInfos) {
            StreamInfoDto dto = BeanUtil.toBean(streamInfo, StreamInfoDto.class);
            dtos.add(dto);
        }
        return Result.newListInstance(StreamInfoDto.class).setData(dtos);
    }

    /**
     * 修改流程详细信息名称
     * @param streamInfoId 流程详细信息编号
     * @param streamInfoName 流程详细信息名称
     * @return result 是否正常访问接口结果
     */
    @GetMapping("/changeStreamInfo.st")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "streamInfoId", value = "流程详细信息编号"),
            @ApiImplicitParam(name = "streamInfoName", value = "流程详细信息名称")
    })
    @ApiOperation("修改流程详细信息名称")
    public Result changeStreamInfo(Integer streamInfoId,String streamInfoName){
        StreamInfo streamInfo = streamInfoService.getById(streamInfoId);
        if(ObjectUtil.isEmpty(streamInfo)) throw new WoniuException("流程详细信息不存在",10001);
        streamInfo.setStreamInfoName(streamInfoName);
        streamInfoService.updateById(streamInfo);
        return Result.getResult().setMsg("修改流程详细信息名称成功");
    }
    /**
     * 删除流程详细信息 （数据删除）
     * @param streamInfoId 流程详细信息编号
     * @return result 是否正常访问接口结果
     */
    @GetMapping("/deleteStreamInfo.st")
    @ApiImplicitParam(name = "streamInfoId", value = "流程详细信息编号")
    @ApiOperation("删除流程详细信息")
    public Result deleteStreamInfo(Integer streamInfoId){
        StreamInfo streamInfo = streamInfoService.getById(streamInfoId);
        if(ObjectUtil.isEmpty(streamInfo)) throw new WoniuException("流程详细信息不存在",10001);
        streamInfoService.removeById(streamInfo);
        return Result.getResult().setMsg("删除流程详细信息成功");
    }
}

